module Tuscan
  module Iec60584
    module TypeR
      T90_RANGE = -50.00..1768.10
      EMF_RANGE = -0.227..21.103 

      extend self

      def emfr_unbound t90
        if t90 < 1064.18
          Polynomial.new(
             0.000000000000e+00,  0.528961729765e-02,  0.139166589782e-04, -0.238855693017e-07,
             0.356916001063e-10, -0.462347666298e-13 , 0.500777441034e-16, -0.373105886191e-19,
             0.157716482367e-22, -0.281038625251e-26
          ).solve_for t90
        elsif t90 < 1664.50
          Polynomial.new(
             0.295157925316e+01, -0.252061251332e-02,  0.159564501865e-04, -0.764085947576e-08,
             0.205305291024e-11, -0.293359668173e-15
          ).solve_for t90
        else
          Polynomial.new(
             0.152232118209e+03, -0.268819888545e+00,  0.171280280471e-03, -0.345895706453e-07,
            -0.934633971046e-14 
          ).solve_for t90
        end
      end

      def t90r_guess emf
        if emf < 1.923
          Polynomial.new(
             0.0000000e+00,  1.8891380e+02, -9.3835290e+01,  1.3068619e+02, -2.2703580e+02,
             3.5145659e+02, -3.8953900e+02,  2.8239471e+02, -1.2607281e+02,  3.1353611e+01,
            -3.3187769e+00
          ).solve_for emf
        elsif emf < 13.228
          Polynomial.new(
             1.334584505e+01,  1.472644573e+02, -1.844024844e+01,  4.031129726e+00, -6.249428360e-01,
             6.468412046e-02, -4.458750426e-03,  1.994710149e-04, -5.313401790e-06,  6.481976217e-08
          ).solve_for emf
        elsif emf < 19.739
          Polynomial.new(
            -8.199599416e+01,  1.553962042e+02, -8.342197663e+00,  4.279433549e-01, -1.191577910e-02,
             1.492290091e-04
          ).solve_for emf
        else
          Polynomial.new(
             3.406177836e+04, -7.023729171e+03,  5.582903813e+02, -1.952394635e+01,  2.560740231e-01
          ).solve_for emf
        end
      end
    end
  end
end

